package com.zm.admin.zmeng.controller;

import cn.dev33.satoken.secure.SaSecureUtil;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.StrUtil;
import com.anji.captcha.model.common.ResponseModel;
import com.anji.captcha.model.vo.CaptchaVO;
import com.anji.captcha.service.CaptchaService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.zm.admin.annotation.SysLogTag;
import com.zm.admin.utils.ConstantEntity;
import com.zm.admin.utils.ServletUtil;
import com.zm.admin.utils.UserIpUtil;
import com.zm.admin.zmeng.domain.ZmUser;
import com.zm.admin.zmeng.mapper.ZmMenuMapper;
import com.zm.admin.zmeng.mapper.ZmUserMapper;

import com.zm.admin.utils.ResultBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/***
 *
 * @Auther: MuYun
 * @Date: 2022/6/20 10:57
 * @Description： 登录接口
 *
 */
@RestController
public class LoginController {

    private static Logger logger= LoggerFactory.getLogger(LoginController.class);

    @Resource
    private ZmUserMapper zmUserMapper;

    @Resource
    private ZmMenuMapper zmMenuMapper;

    @Autowired
    private CaptchaService captchaService;

    /**
     * 登录
     * @param username
     * @param password
     * @return
     */
    @RequestMapping("/doLogin")
    @SysLogTag(desc = " 登陆系统",type = ConstantEntity.LOGIN)
    public ResultBean login(@RequestParam(value = "username") String username,
                            @RequestParam(value = "password") String password,
                            @RequestParam(value = "captcha") String captchaVerification){
        ResultBean resultBean=new ResultBean();
        //logger.info("进来了，参数为{},{},{}",username,password,captchaVerification);
        CaptchaVO captchaVO=new CaptchaVO();
        if(StrUtil.isEmpty(captchaVerification)){
            resultBean.setCode(ResultBean.CODE_ERROR).setMsg("验证码出现了点问题..，请稍后再试！");
            return resultBean;
        }
        captchaVO.setCaptchaVerification(captchaVerification);
        ResponseModel responseModel = captchaService.verification(captchaVO);
        //logger.info("验证码验证情况:{}",responseModel.toString());
        if(!responseModel.isSuccess()){
            resultBean.setCode(ResultBean.CODE_ERROR).setMsg("验证码验证错误，请重试！");
            return resultBean;
        }
        QueryWrapper<ZmUser> wrapper=new QueryWrapper<>();
        ZmUser zmUser = zmUserMapper.selectOne(wrapper.lambda().eq(ZmUser::getUsername,username));
        if(ObjectUtils.isEmpty(zmUser)||!password.equals(SaSecureUtil.aesDecrypt(zmUser.getSalt(),zmUser.getPassword()))){
            resultBean.setCode(500).setMsg("用户号或密码错误，请检查后重试！");
        }else {
            //非记住我登陆，否则登陆日志将记录不全
            StpUtil.login(zmUser.getId(),false);
            String ip = UserIpUtil.getIp(ServletUtil.getRequest());
            zmUser.setLoginIp(ip);
            zmUserMapper.updateById(zmUser);
            resultBean.setCode(200).setData(zmUser).setMsg("登录成功,欢迎您："+zmUser.getUsername());
            if(!StpUtil.isLogin()){
                resultBean.setCode(403);
                resultBean.setMsg("您还未登陆");
            }
        }
        return resultBean;
    }

}
